// LGUIKnob.cpp: implementation of the LGUIKnob class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"

#include "LGUIKnob.h"
#include "LGUIContainer.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

LGUIKnob::LGUIKnob(const TCHAR *_text, int id, int _x, int _y, int _w, int _h, int _value, int _min, int _max, int _userdata, int _style, short16 _bgColor, short16 _lineColor)
: LGUIWindow(_x,_y,_w,_h)
{
	radius=w/2; value=_value; minValue=_min; maxValue=_max; bgColor = _bgColor; lineColor=_lineColor;
	dragging=false;
	ID=id;
	text=_text;
	value=0;
	style=_style;
	dragging=false;
	setUserData(_userdata);
}

LGUIKnob::~LGUIKnob()
{

}


void LGUIKnob::penDown(int px, int py)
{
	if (isInside(px,py))
	{
		switch (style) {
	case KNOB_NORMAL:
		dragging=true;
		lastY=py;
		//break;
	case KNOB_PLUSMINUS:
		if (py-y < h/2) 
			value++;
		else
			value--;
		clamp();
		parent->deliverMessage(LGUIMessage(MSG_KNOBCHANGED,ID,value,userData));
		};
		redraw();
	}
}


void LGUIKnob::penUp(int px, int py)
{
	if (dragging)
		redraw();
	dragging=false;

}

void LGUIKnob::penMove(int px, int py, bool pressed)
{
	if (!dragging) return;
	if (style != KNOB_NORMAL) return;
	int diff=(lastY-py)*(maxValue-minValue)/100;

	if (diff==0 && !((lastY-py)==0))
		diff = lastY < py ? -1 : 1;

	
	/*
	#ifdef WINCE
	if (theApp->isButtonDown(LAMEApp::BUTTON_A))
	#else
	if (GetAsyncKeyState(VK_SHIFT))
	#endif
	{
	if ((lastY-py)==1)
	diff = 1;
	if ((lastY-py)==-1)
	diff = -1;
	}*/

	value += diff;
	clamp();

	if (parent) 
		parent->deliverMessage(LGUIMessage(MSG_KNOBCHANGED,ID,value,userData));

	lastY=py;
	redraw();
}



void LGUIKnob::draw(int xoff, int yoff)
{
	if (isDirty)
	{
		//value=128;
		buffer.rect(x+xoff,y+yoff,x+xoff+w,y+yoff+h,dragging ? COLOR(0x80ff80) : COLOR(0x404040));
		buffer.rectFillFaded(x+xoff+1,y+yoff+1,x+xoff+w,y+yoff+h,COLOR(0x505050),COLOR(0x808080));
		int valY = y+yoff+h-(h-1)*(value-minValue)/(maxValue-minValue);

		buffer.rectFillFaded(x+xoff+1,valY,x+xoff+w,y+yoff+h,dragging ? COLOR(0xff5050) : COLOR(0xff4040),COLOR(0xa04040));
		if (dragging)
			buffer.hLine(x+xoff+1,valY,x+xoff+w,COLOR(0x80ff80));

		if (style==KNOB_PLUSMINUS)
		{
			buffer.hLineAlpha50(x+xoff+1,y+yoff+h/2,x+xoff+w,0xffc0);
			buffer.drawTextCenter(TEXT("+"), x+xoff+w/2-1,y+yoff+5, 0xffff,1);
			buffer.drawTextCenter(TEXT("-"), x+xoff+w/2-1,y+yoff+h-5, 0xffff,1);
		}

		buffer.drawTextCenter(text, x+xoff+w/2-1,y+yoff+h+7, 0x0, 0);
		TCHAR temp[8];
		_stprintf(temp,TEXT("%i"),value);
		buffer.drawTextShadowCenter(temp,x+xoff+w/2-1,y+yoff+h/2,0xffff);
		LRect dirty = *this;
		dirty.x+=xoff;
		dirty.y+=yoff;
		dirty.h+=8;
		dirty.w++;
		buffer.addDirtyRect(dirty);

		isDirty = false;
	}
}

void LGUIKnob::setValue(int v)
{
	if (minValue == 0 && v < 0 && maxValue>127) //likely unsigned
		v = (int)(unsigned char)(char)v;

	int oldv=value;
	value=v;
	clamp();
	if (oldv!=value)
		redraw();
}
